﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Common_AgentManageSelect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            InitEditControls();
            LoadData();
        }
    }

    void InitEditControls()
    {
        this.htxtRole.Value = Request.QueryString["role"];
        this.htxtAgent.Value = Request.QueryString["agent"];
        this.htxtSDate.Value = Request.QueryString["sdate"];
        this.htxtEDate.Value = Request.QueryString["edate"];
        this.htxtParAgent.Value = Request.QueryString["paragent"];
        this.htxtRemark.Value = Request.QueryString["remark"];

        this.ckbAll.Attributes.Add("onclick","return ckbAll_onclick(this,event);");
        this.ibtnCancel.Attributes.Add("onclick", "return close_page(this,event);");
    }

    DataTable g_dtSys_Roles_Agent;
    void LoadData()
    {
        //提取该角色已与此用户已存在的代理流程
        string _sql = "";
        _sql="Select Flow_Desc "+
            "From dbo.SYS_ROLES_AGENT "+
            "Where Role_ID='" + this.htxtRole.Value + "' And AGENT='" + this.htxtAgent.Value + "'";
        g_dtSys_Roles_Agent = Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",
                            _sql,Srvtools.CliUtils.fLoginDB, true, Srvtools.CliUtils.fCurrentProject).Tables[0];
        this.dltFlow.DataSource = this.FlowDes;
        this.DataBind();
    }

    protected void dltFlow_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox _ckb = e.Item.FindControl("ckbFlow") as CheckBox;
            _ckb.Attributes.Add("onclick","return ckbFlow_onclick(this,event);");
            string _flow_des = (e.Item.DataItem as DataRowView).Row["Des"].ToString().Trim();
            if (this.g_dtSys_Roles_Agent.Select("Flow_Desc='" + _flow_des + "'").Length > 0)
            {                
                _ckb.Checked = true;
                _ckb.Font.Bold = true;
            }
        }
    }

    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        string _Role = this.htxtRole.Value;
        string _Agent = this.htxtAgent.Value;
        string _SDate = this.htxtSDate.Value.Replace("-", "");
        string _STime = "000000";
        string _EDate = this.htxtEDate.Value.Replace("-", "");
        string _ETime = "235959";
        string _ParAgent = this.htxtParAgent.Value;
        string _Remark = this.htxtRemark.Value;
        System.Text.StringBuilder _sb = new System.Text.StringBuilder();
        foreach (DataListItem _li in this.dltFlow.Items)
        {
            CheckBox _ckb = _li.FindControl("ckbFlow") as CheckBox;
            if (_ckb.Checked && _ckb.Text.Trim()!="")
                _sb.Append(_ckb.Text + ";");
        }
        string _Flows = _sb.ToString();
        object[] back = null;
        back = Srvtools.CliUtils.CallMethod("Common", "Insert_Sys_Roles_Agent",
            new object[] { _Role, _Agent, _SDate, _STime, _EDate, _ETime, _ParAgent, _Flows, _Remark });
        //执行成功
        if (back[1].ToString() == "Y")
        {           
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "refreshParentPage();", true);
        }
    }
}
